package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.VipUser;
import com.ricohgag.mapper.VipUserMapper;
import com.ricohgag.service.VipUserService;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements VipUserService {
    @Autowired
    private VipUserMapper vipUserMapper;

    @Override
    public IPage<VipUser> selectVipUserPage(String name, Page<VipUser> page, String startDate, String endDate) throws Exception{
        QueryWrapper<VipUser> qw = new QueryWrapper<>();
        if(name != null){
            qw.like("username", name);
        }
        if(startDate!=null && endDate!=null){
            qw.between("add_time", startDate, endDate);

        }

        IPage<VipUser> vipUserIPage = vipUserMapper.selectPage(page, qw);
        Assert.assertSame(page, vipUserIPage);

        return vipUserIPage;
    }

    @Override
    public VipUser selectByName(String username)throws Exception{
        QueryWrapper<VipUser> qw = new QueryWrapper<>();
        qw.eq("username", username);
        List<VipUser> vipUserList = vipUserMapper.selectList(qw);
        VipUser vipUser = vipUserList.size()>0?vipUserList.get(0):null;
        return vipUser;
    }

    @Override
    public VipUser checkVipUser(String username, String password) throws Exception{
        VipUser vipUser = selectByName(username);

        if(vipUser!=null && !"".equals(username.trim())){
            if(password.equals(vipUser.getPassword())){
                return vipUser;
            }
            return null;
        }
        return null;
    }

    @Override
    public VipUser selectVipUserOne(Integer id) throws Exception{

        return vipUserMapper.selectById(id);
    }

    @Override
    public int updateVipUser(VipUser vipUser) throws Exception{
        return vipUserMapper.updateById(vipUser);
    }

    @Override
    public int insertVipUser(VipUser vipUser) throws Exception{
        vipUser.setAddTime(LocalDateTime.now());
        return vipUserMapper.insert(vipUser);
    }

    @Override
    public int deleteVipUser(int id) throws Exception{
        return vipUserMapper.deleteById(id);
    }
}
